//
//  DrugCountPickerView.m
//  datePickerView
//
//  Created by lianchuang on 14-11-21.
//  Copyright (c) 2014年 lianchuang. All rights reserved.
//

#import "DrugCountPickerView.h"

 

@interface DrugCountPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
    NSArray *_units;
    NSMutableArray *_countsArray;

}





@end

@implementation DrugCountPickerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self makePickerView];
    }
    return self;
}
-(void)makePickerView
{
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.sureButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.sureButton.frame = CGRectMake(self.frame.size.width-75, 10, 60, 30);
    [self.sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [self addSubview:self.sureButton];
    //
    self.cancleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.cancleButton.frame = CGRectMake(15, 10, 60, 30);
    [self.cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [self addSubview:self.cancleButton];
    
    
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, self.width, 200)];
    [self addSubview:self.pickerView];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    
    _units = @[@"颗",@"片",@"袋",@"瓶",@"毫升",@"haha"];
    _countsArray = [NSMutableArray arrayWithCapacity:0];
    [_countsArray addObject:@"0.5"];
    for (NSInteger i = 0; i <22 ;i++ ) {
        [_countsArray addObject:[NSString stringWithFormat:@"%ld",i+1]];
    }
    
    
    
    _unitNum = 0;
    _countNum = 0;
    [self.pickerView selectRow:[self calculateMiddleIndexWithFirstIndex:self.unitNum circulateTimes:UNIT_CIRCULATE_TIMES elementCount:UNIT_COUNT] inComponent:1 animated:NO];
    [self.pickerView selectRow:[self calculateMiddleIndexWithFirstIndex:self.countNum circulateTimes:DOSE_CIRCULATE_TIMES elementCount:DOSE] inComponent:0 animated:NO];
    
    
    
}
-(void)btndown
{
//    NSLog(@"%d,%d",_unitNum,_countNum);
//    NSLog(@"%@,%@",_units[_unitNum],_countsArray[_countNum]);

}
-(NSString*)getDrugDose
{
    NSString * str = [NSString stringWithFormat:@"%@ %@",_countsArray[_countNum],_units[_unitNum]];
    return str;

}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return DOSE_CIRCULATE_TIMES*DOSE;
            break;
            
        case 1:
            
            return UNIT_CIRCULATE_TIMES* UNIT_COUNT;//循环100组，每一组5个元素
            break;
            
        default:
            return 0;
            break;
    }

}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
-(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = nil;
    label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 58, 35)];
    label.font = [UIFont boldSystemFontOfSize:13.f];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    switch (component) {
        case 0:
            label.text = [NSString stringWithFormat:@"%@", _countsArray[row%DOSE]];
            break;
            
        case 1:
            label.text = [NSString stringWithFormat:@"%@", _units[row%UNIT_COUNT]];
            break;
            
        default:
            return nil;
            break;
    }
    return label;

}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    switch (component) {
        case 0:
            _countNum = row % DOSE;
            [pickerView selectRow:[self calculateMiddleIndexWithFirstIndex:self.countNum circulateTimes:DOSE_CIRCULATE_TIMES elementCount:DOSE] inComponent:0 animated:NO];
            
            break;
            
        case 1:
            
            //
            _unitNum = row % UNIT_COUNT;
            [pickerView selectRow:[self calculateMiddleIndexWithFirstIndex:self.unitNum circulateTimes:UNIT_CIRCULATE_TIMES elementCount:UNIT_COUNT] inComponent:1 animated:NO];
            
            break;
            
        default:
            break;
    }
    
}


-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (pickerView == self.pickerView) {
        return 90.0;
    }else
    {
        return 0;
    }

}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    if ([pickerView isEqual:self.pickerView]) {
        return 30.0;
    }else
    {
        return 0;
    }
}

- (NSInteger)calculateMiddleIndexWithFirstIndex:(NSInteger)firstIndex circulateTimes:(NSInteger)times elementCount:(NSInteger)count
{
    if (times % 2 != 0) {
        times--;
        
    }
    NSInteger index = firstIndex + times / 2 * count;
    return index;
}


@end
